package com.myhome.money.base.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author 许路路
 */
public class ChannelTransfer {

    public static void main(String[] args) throws IOException {
        fromChannel();

        toChannel();
    }

    public static void fromChannel() throws IOException {

        RandomAccessFile accessFile = new RandomAccessFile("D:/javaio/nio/fromFile.txt", "rw");
        FileChannel fileChannel = accessFile.getChannel();
        RandomAccessFile toFile = new RandomAccessFile("D:/javaio/nio/toFile.txt", "rw");
        FileChannel toChannel = toFile.getChannel();
        long position = 0;
        long count = fileChannel.size();
        fileChannel.transferTo(position, count, toChannel);

    }


    public static void toChannel() throws IOException {

        RandomAccessFile accessFile = new RandomAccessFile("D:/javaio/nio/fromFile.txt", "rw");
        FileChannel fromChannel = accessFile.getChannel();

        RandomAccessFile tofile = new RandomAccessFile("D:/javaio/nio/toFile1.txt", "rw");
        FileChannel toChannel = tofile.getChannel();

        long position = 0;
        long count = fromChannel.size();
        toChannel.transferFrom(fromChannel, position, count);


    }

}
